package com.example.stlink.utils.activityUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.example.stlink.model.constants.ModelFieldConstants;
import com.example.stlink.model.constants.UrlConstants;
import com.example.stlink.model.response.BaseResponse;
import com.example.stlink.utils.httpUtils.PostBodyParamHttpUtils;
import com.example.stlink.utils.httpUtils.PostFileHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class ImgUploadUtil {

    private static Bundle mBundle;
    private static Handler mHandler;
    private static String mImgId;

    public static void userImgUpload(Bundle bundle, Handler handler, @Nullable String path, String imgId) {

        mBundle = bundle;
        mHandler = handler;
        mImgId = imgId;

        mBundle.getBoolean(UrlConstants.IS_IMG_UPLOAD_SUCCESS, false);

        new Thread(() -> {
            File file = null;
            if (path != null && !TextUtils.isEmpty(path)) {
                file = new File(path);
            }
            new PostFileHttpUtils() {
                @Override
                public void nextMethod(BaseResponse<Object> baseResponse) {
                    if(ModelFieldConstants.AVATAR.equals(mImgId)){
                        userImgUploadNext(baseResponse);
                    }else if (ModelFieldConstants.COURSE_PHOTO.equals(mImgId)){
                        Message message = new Message();
                        mBundle.putString(mImgId, (String) baseResponse.getData());
                        mBundle.putBoolean(UrlConstants.IS_IMG_UPLOAD_SUCCESS, true);
                        message.obj = mBundle;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void failedMethod(BaseResponse<Object> baseResponse) {
                    Message message = new Message();
                    System.out.println("上传失败.......................");
                    handler.sendMessage(message);
                }
            }.FileUpload(UrlConstants.FILE_UPLOAD, file);
        }).start();
    }

    public static void userImgUploadNext(BaseResponse<Object> baseResponse){
        //构建mapBody
        String imgUrl = (String) baseResponse.getData();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(ModelFieldConstants.ID, (Long) mBundle.get(ModelFieldConstants.ID));
            jsonObject.put(mImgId, imgUrl);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String json = jsonObject.toString();
        new PostBodyParamHttpUtils() {
            @Override
            public void nextMethod(BaseResponse<Object> baseResponse) {
                Message message = new Message();
                mBundle.putString(mImgId, imgUrl);
                mBundle.putBoolean(UrlConstants.IS_IMG_UPLOAD_SUCCESS, true);
                message.obj = mBundle;
                mHandler.sendMessage(message);
            }
            @Override
            public void failedMethod(BaseResponse<Object> baseResponse) {
                Message message = new Message();
                System.out.println("修改失败.......................");
                mBundle.putString(mImgId, imgUrl);
                mBundle.putBoolean(UrlConstants.IS_IMG_UPLOAD_SUCCESS, false);
                message.obj = mBundle;
                mHandler.sendMessage(message);
            }
        }.postConnect(UrlConstants.USER_UPDATE, json);
    }
}
